<?php

namespace App\Http\Controllers\Innerend;


use App\Models\Event;
use App\Models\User;

class EventController extends BaseController
{

    // 用户
    public function initialize()
    {
        parent::initialize();

        $this->model = 'Event';
    }


    // 列表
    // 添加
    // 修改
    // 删除
    // 推荐
    // 评论列表

    // 推荐
    public function onPropose()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $result = $manage->onPropose();
        if($result === false){
            return $this->error($manage->getError());
        }

        return $this->success();
    }


    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }

        // 展示列表表格
        $sexes    = [1 => '男', 2 => '女', 0 => '保密'];
        $verifys  = $this->verifys();
        $types    = (new Event())->getType(false);
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_id = $item['id'];
            //$status   = $item['status'] == 1 ? '正常' : '禁用';
            //$statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            // 推荐
            $operate = '';
            $operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['is_propose']], $item['is_propose'] == 1 ? '取消推荐' : '推荐', $item['is_propose'], 'on-propose');
            //$operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);

            // 审核操作
            if($item['verify'] == 0){
                $operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1], '审核通过', 1, 'on-verify');
                $operate .= $listHtml->operate_status(['id' => $_id, 'value' => 2], '审核不通过', 2, 'on-verify');
            }
            $operate .= $listHtml->operate_edit(['id' => $_id], '编辑');
            //$operate .= $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该用户吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($types[$item['event_type']] ?? '');
            $tr .= $listHtml->list_td($item['event_name']);
            $tr .= $listHtml->list_td($item['cost']);
            $tr .= $listHtml->list_date($item['dead_time']);
            $tr .= $listHtml->list_date($item['end_time']);
            $tr .= $listHtml->list_td($item['is_refund'] == 1 ? '是' : '否');
            $tr .= $listHtml->list_td($item['is_propose'] == 1 ? '是' : '否');
            $tr .= $listHtml->list_td($verifys[$item['verify']] ?? '-');
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate);

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl     = Url('/innerend/event/add');
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加活动</a> ';
        $this->assign('top_button', $top_button);
    }

    // 列表字段
    protected function list_fields()
    {
        return ['event_type' => '活动类型', 'event_name' => '活动名称', 'cost' => '费用', 'dead_time' => '报名截止时间', 'end_time' => '结束时间', 'is_refund' => '是否支持退款', 'is_propose' => '是否推荐', 'verify' => '状态', 'ctime' => '创建时间', 'operate' => '操作',];
    }

    // 列表搜索
    protected function list_search()
    {
        $model   = new Event();
        $request = $this->optLogic()->getData();
        $form    = $this->adminList();
        $tyeps   = $model->getType(false);

        $verify     = $form->formSelect('verify', $request['verify'] ?? null, $this->verifys());
        $isPropose  = $form->formSelect('is_propose', $request['is_propose'] ?? null, ['1' => '推荐', 0 => '未推荐']);
        $event_type = $form->formSelect('event_type', isset($request['event_type']) ? ($tyeps[$request['event_type']] ?? null) : null, $tyeps);
        $event_name = $form->formInput('event_name', $request['event_name'] ?? null, '认证名称');
        $uid        = $form->formInput('uid', $request['uid'] ?? null, '用户ID', '120px');
        $search     = ['verify' => $verify, 'is_propose' => $isPropose, 'uid' => $uid, 'event_name' => $event_name,]; // 'event_type' => $event_type,
        $this->assign('search', $search);


        $searchHide = ['event_type' => $request['event_type'] ?? null];
        $this->assign('searchHide', $searchHide);
    }

    // 表单页面
    protected function formTable()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();

        $verifys = $this->verifys();
        $types   = (new Event())->getType(false);

        // 用户
        $uInfo = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);

        // 用户名
        //$uid    = $form->ajaxSearch('uid', $info['uid'] ?? '', 'User');
        $uid    = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');
        $status = $form->formRadio('status', $info['status'] ?? 1, [1 => '正常', 0 => '禁用']);
        $event_type  = $form->formRadio('event_type', isset($info['event_type']) ? ($types[$info['event_type']] ?? '') : '', $types);
        $event_name  = $form->formInput('event_name', $info['event_name'] ?? '', '活动名称');
        $price       = $form->formInput('price', $info['price'] ?? '', '市场价', '120px');
        $cost        = $form->formInput('cost', $info['cost'] ?? '', '成本价', '120px');
        $dead_time   = $form->formDateTime('dead_time', $info['dead_time'] ?? '', '报名截止时间', 'datetime');
        $start_time  = $form->formDateTime('start_time', $info['start_time'] ?? '', '开始时间', 'datetime');
        $end_time    = $form->formDateTime('end_time', $info['end_time'] ?? '', '结束时间', 'datetime');
        $city        = $form->formInput('city', $info['city'] ?? '', '城市');
        $address     = $form->formInput('address', $info['address'] ?? '', '地址');
        $description = $form->formTextarea('description', $info['description'] ?? '');
        $cover       = $form->formImage('cover', $info['cover'] ?? '');
        $images      = $form->formImage('images', $info['images'] ?? '');
        $is_refund   = $form->formRadio('is_refund', $info['is_refund'] ?? 1, [1 => '是', 0 => '否']);
        $verify      = $form->formRadio('verify', $info['verify'] ?? 1, $verifys);
        $_id         = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        //$html .= $form->makeItem('状态', $status);
        $html .= $form->makeItem('用户', $uid);
        $html .= $form->makeItem('活动类型', $event_type);
        $html .= $form->makeItem('活动名称', $event_name);
        $html .= $form->makeItem('城市', $city);
        $html .= $form->makeItem('地址', $address);
        $html .= $form->makeItem('市场价', $price);
        $html .= $form->makeItem('成本价', $cost);
        $html .= $form->makeItem('报名截止时间', $dead_time);
        $html .= $form->makeItem('开始时间', $start_time);
        $html .= $form->makeItem('结束时间', $end_time);
        $html .= $form->makeItem('封面图片', $cover);
        //$html .= $form->makeItem('图片列表', $images);
        $html .= $form->makeItem('是否支持退款', $is_refund);
        $html .= $form->makeItem('审核状态', $verify);
        $html .= $form->makeItem('认证描述', $description);
        $html .= $_id;

        return $html;
    }

    // 审核状态
    protected function verifys()
    {
        return [2 => '审核未通过', 1 => '审核通过', 0 => '未审核',];
    }

}


